"""
基于GenericAPIView结合5个视图扩展类完成基本的5个API接口
ListModelMixin     提供了list方法，获取多条数据
CreateModelMixin   提供了create方法，添加一条数据

RetrieveModelMixin 提供了retrieve方法，获取一条数据
UpdateModelMixin   提供了update方法，更新一条数据
DestroyModelMixin   提供了destroy方法，删除一条数据
"""

from rest_framework.generics import GenericAPIView
from rest_framework.mixins import ListModelMixin, CreateModelMixin, RetrieveModelMixin, UpdateModelMixin, \
    DestroyModelMixin

from sers.models import Book
from sers.serializer import BookSerializer3


class BookView(GenericAPIView, ListModelMixin, CreateModelMixin):
    queryset = Book.objects.all()
    serializer_class = BookSerializer3

    def get(self, request):
        return self.list(request)

    def post(self, request):
        return self.create(request)


class BookDetailView(GenericAPIView, RetrieveModelMixin, UpdateModelMixin, DestroyModelMixin):
    queryset = Book.objects.all()
    serializer_class = BookSerializer3

    def get(self, request, pk):
        return self.retrieve(request, pk)

    def put(self, request, pk):
        return self.update(request, pk)

    def delete(self, request, pk):
        return self.destroy(request, pk)